package com.gmss.crawler.module.researchproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gmss.crawler.constants.Base;
import com.gmss.crawler.constants.Constants;
import com.gmss.crawler.enums.CommonEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import com.gmss.crawler.module.researchproject.service.IResearchProjectService;
import com.gmss.crawler.module.researchproject.domain.ResearchProject;

import java.util.*;

import lombok.Data;


/**
 * <p>
 * 科研项目表 前端控制器
 * </p>
 *
 * @author Auto-generator
 * @since 2021-12-06
 */
@Slf4j
@RestController
@RequestMapping("/researchProject")
public class ResearchProjectController extends Base {


    @Autowired
    private IResearchProjectService iResearchProjectService;


    /**
     * 通过id查询
     */
    @GetMapping("/getById/{id}")
    public String getById(@PathVariable(value = "id") Integer id) {
        return respJson(CommonEnum.SUCCESS, iResearchProjectService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public String save(@RequestBody ResearchProject researchProject) {
        iResearchProjectService.save(researchProject);
        return respJson(CommonEnum.SUCCESS);
    }

    /**
     * 通过id删除
     */
    @DeleteMapping("/deleteById/{id}")
    public String delete(@PathVariable(value = "id") String ids) {
        String[] idsStrs = ids.split(",");
        for (String id : idsStrs) {
            iResearchProjectService.removeById(Integer.parseInt(id));
        }
        return respJson(CommonEnum.SUCCESS);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public String updateById(@RequestBody ResearchProject researchProject) {
        iResearchProjectService.updateById(researchProject);
        return respJson(CommonEnum.SUCCESS);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    public String list(@RequestBody ResearchProjectReqVo researchProject) {
        final LambdaQueryWrapper<ResearchProject> lambda = new QueryWrapper<ResearchProject>().lambda();
        this.buildCondition(lambda, researchProject);
        return respJson(CommonEnum.SUCCESS, iResearchProjectService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public String page(@RequestBody ResearchProjectReqVo pageParam) {

        final LambdaQueryWrapper<ResearchProject> lambda = new QueryWrapper<ResearchProject>().lambda();
        this.buildCondition(lambda, pageParam);
        final IPage<ResearchProject> page = iResearchProjectService.page(new Page<>(Constants.PAGE_NO, Constants.PAGE_SIZE), lambda);
        return respJson(CommonEnum.SUCCESS, page);
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<ResearchProject> lambda, ResearchProjectReqVo param) {
        if (null != param.getId()) {
            lambda.eq(ResearchProject::getId, param.getId());
        }
        if (null != param.getPersonId()) {
            lambda.eq(ResearchProject::getPersonId, param.getPersonId());
        }
        if (!StringUtils.isEmpty(param.getType())) {
            lambda.eq(ResearchProject::getType, param.getType());
        }
        if (!StringUtils.isEmpty(param.getLanguageStandard())) {
            lambda.eq(ResearchProject::getLanguageStandard, param.getLanguageStandard());
        }
        if (!StringUtils.isEmpty(param.getTitleZh())) {
            lambda.eq(ResearchProject::getTitleZh, param.getTitleZh());
        }
        if (!StringUtils.isEmpty(param.getApplyNumber())) {
            lambda.eq(ResearchProject::getApplyNumber, param.getApplyNumber());
        }
        if (!StringUtils.isEmpty(param.getRegisterNumber())) {
            lambda.eq(ResearchProject::getRegisterNumber, param.getRegisterNumber());
        }
        if (!StringUtils.isEmpty(param.getKeywords())) {
            lambda.eq(ResearchProject::getKeywords, param.getKeywords());
        }
        if (!StringUtils.isEmpty(param.getKeywordsZh())) {
            lambda.eq(ResearchProject::getKeywordsZh, param.getKeywordsZh());
        }
        if (!StringUtils.isEmpty(param.getCurrency())) {
            lambda.eq(ResearchProject::getCurrency, param.getCurrency());
        }
        if (null != param.getAmount()) {
            lambda.eq(ResearchProject::getAmount, param.getAmount());
        }
        if (!CollectionUtils.isEmpty(param.getStartYearList())) {
            lambda.ge(ResearchProject::getStartYear, param.getStartYearList().get(0));
            lambda.le(ResearchProject::getStartYear, param.getStartYearList().get(1));
        }
        if (!CollectionUtils.isEmpty(param.getStartDateList())) {
            lambda.ge(ResearchProject::getStartDate, param.getStartDateList().get(0));
            lambda.le(ResearchProject::getStartDate, param.getStartDateList().get(1));
        }
        if (!CollectionUtils.isEmpty(param.getEndYearList())) {
            lambda.ge(ResearchProject::getEndYear, param.getEndYearList().get(0));
            lambda.le(ResearchProject::getEndYear, param.getEndYearList().get(1));
        }
        if (!CollectionUtils.isEmpty(param.getEndDateList())) {
            lambda.ge(ResearchProject::getEndDate, param.getEndDateList().get(0));
            lambda.le(ResearchProject::getEndDate, param.getEndDateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getAbstractEn())) {
            lambda.eq(ResearchProject::getAbstractEn, param.getAbstractEn());
        }
        if (!StringUtils.isEmpty(param.getAbstractZh())) {
            lambda.eq(ResearchProject::getAbstractZh, param.getAbstractZh());
        }
        if (!StringUtils.isEmpty(param.getDescZh())) {
            lambda.eq(ResearchProject::getDescZh, param.getDescZh());
        }
        if (!CollectionUtils.isEmpty(param.getCreateTimeList())) {
            lambda.ge(ResearchProject::getCreateTime, param.getCreateTimeList().get(0));
            lambda.le(ResearchProject::getCreateTime, param.getCreateTimeList().get(1));
        }
        if (!StringUtils.isEmpty(param.getCreateUser())) {
            lambda.eq(ResearchProject::getCreateUser, param.getCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getUpdateTimeList())) {
            lambda.ge(ResearchProject::getUpdateTime, param.getUpdateTimeList().get(0));
            lambda.le(ResearchProject::getUpdateTime, param.getUpdateTimeList().get(1));
        }
        if (!StringUtils.isEmpty(param.getUpdateUser())) {
            lambda.eq(ResearchProject::getUpdateUser, param.getUpdateUser());
        }
        lambda.orderBy(true, false, ResearchProject::getId);
    }


    /**
     * 请求model
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class ResearchProjectReqVo extends ResearchProject {
        private List<String> startYearList; // 开始年份起止
        private List<String> startDateList; // 开始时间起止
        private List<String> endYearList; // 结束年份起止
        private List<String> endDateList; // 结束时间起止
        private List<String> createTimeList; // 创建时间起止
        private List<String> updateTimeList; // 更新时间起止
    }


}
